package com.polaris.live.ui.live.widget

import android.content.Context
import android.text.TextUtils
import android.util.AttributeSet
import android.view.LayoutInflater
import android.widget.RelativeLayout
import com.bumptech.glide.GenericTransitionOptions
import com.bumptech.glide.Glide
import com.polaris.live.common.constant.PagConst.LIVE_LOADING_PAG
import com.polaris.live.databinding.LiveLoadingVideoBinding
import com.polaris.live.utils.image.loadImage
import com.polaris.live.utils.image.normal
import jp.wasabeef.glide.transformations.BlurTransformation

/**
 * LiveVideoLoadingView
 *
 * @author Created by lucas on 2023/11/25 15:04
 */
class LiveVideoLoadingView(context: Context, attributeSet: AttributeSet? = null) :
    RelativeLayout(context, attributeSet) {

    private val mBinding = LiveLoadingVideoBinding.inflate(LayoutInflater.from(context), this)

    init {
        mBinding.pagView.apply {
            path = LIVE_LOADING_PAG
            setRepeatCount(-1)
        }
    }

    fun loadCover(coverUrl: String, loadingString: String? = null) {
        Glide.with(context)
            .loadImage(coverUrl)
            .transition(GenericTransitionOptions.withNoTransition())
            .transform(BlurTransformation(25, 4))
            .normal()
            .into(mBinding.ivCover)

        if (!TextUtils.isEmpty(loadingString)) {
            mBinding.tvText.text = loadingString
            mBinding.tvText.visibility = VISIBLE
        }



        mBinding.llLoading.visibility = VISIBLE
        mBinding.pagView.play()
    }

    fun hideLoading() {
        mBinding.llLoading.visibility = GONE
        mBinding.pagView.pause()
    }
}